package priv.mill.ws;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;

import org.apache.log4j.Logger;
@ServerEndpoint("/zhangsan")
public class WebSocketTest  {
	public static Logger logger = Logger.getLogger(WebSocketTest.class);
	private static final Map<Session, Object> map=new HashMap<Session, Object>();
//	@OnOpen
	public void start(){
		logger.debug("客户端来访！");
	}
	
	@OnOpen
	public void start2(Session session){
		logger.debug(map+"--"+map.size());
		map.put(session, this);
		logger.debug(session);
	}
	
	@OnMessage
	public void message(String msg,Session session){
		
		try {
			sendAll(msg, session);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void sendAll(String msg,Session session) throws Exception{
		for (Entry<Session, Object> item : map.entrySet()) {
			item.getKey().getBasicRemote().sendText("服务器收到"+msg);
		}
	}
}
